package com.srwl.mytx.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RightMenuPopupWindow extends PopupWindow implements View.OnClickListener {

    private View.OnClickListener listener;
    private RecyclerView rv_menuList;
    private Context context;
    private List<String> menuItems;

    public RightMenuPopupWindow(Context context, List<String> menuItems) {
        super(context);
        this.context = context;
        this.menuItems = menuItems;
        initView(context);

    }

    public RightMenuPopupWindow(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RightMenuPopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.right_menu_layout, null);
        rv_menuList = view.findViewById(R.id.rv_menu_list);
        rv_menuList.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        rv_menuList.setAdapter(new MyAdapter());
        //设置ChooseRecordPopupWindow的View
        this.setContentView(view);
        //设置ChooseRecordPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置ChooseRecordPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置ChooseRecordPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置弹出窗体的背景
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }


    @Override
    public void onClick(View v) {

//        switch (v.getId()) {
//            case R.id.rl_expose:
//                listener.onSelectTopicType(Constant.TOPIC_TYPE_EXPOSE);
//                break;
//            case R.id.rl_lost_people:
//                listener.onSelectTopicType(Constant.TOPIC_TYPE_LOST_PEOPLE);
//                break;
//            case R.id.rl_lost_thing:
//                listener.onSelectTopicType(Constant.TOPIC_TYPE_REWARD);
//                break;
//            case R.id.rl_found:
//                listener.onSelectTopicType(Constant.TOPIC_TYPE_FOUND);
//                break;
//            case R.id.rl_gift:
//                listener.onSelectTopicType(Constant.TOPIC_TYPE_GIFT);
//                break;
//            case R.id.rl_show:
//                listener.onSelectTopicType(Constant.TOPIC_TYPE_SHOW);
//                break;
//            case R.id.rl_goods:
//                listener.onSelectTopicType(Constant.TOPIC_TYPE_GOODS);
//                break;
//            case R.id.rl_auction:
//                listener.onSelectTopicType(Constant.TOPIC_TYPE_AUCTION);
//                break;
//        }
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {


        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(context).inflate(R.layout.right_menu_item, viewGroup, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
            myViewHolder.tv_dialogItem.setText(menuItems.get(i));
            myViewHolder.itemView.setTag(menuItems.get(i));
            myViewHolder.itemView.setOnClickListener(listener);
        }

        @Override
        public int getItemCount() {
            return menuItems.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_dialog_item)
            TextView tv_dialogItem;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }


    public void setOnClickListener(View.OnClickListener listener) {
        this.listener = listener;
    }


}
